package com.example.robertluozizhao.framecollectdemo.fragment

import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.TouchDelegate
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
import com.example.robertluozizhao.framecollectdemo.databinding.ItemIndicateLayoutBinding

/**
 * Created by robert.luozizhao on 2018/3/5.
 */
class OneFragment : Fragment() {

    val binding:ItemIndicateLayoutBinding by lazy{
        ItemIndicateLayoutBinding.inflate(LayoutInflater.from(activity))
    }

    override fun onCreateView(
        @NonNull inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        /*  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        LinearLayout linearLayout = new LinearLayout(getActivity());
        linearLayout.setLayoutParams(params);

        TextView textView = new TextView(getActivity());
        textView.setText("one");
        textView.setGravity(TextView.TEXT_ALIGNMENT_CENTER);
        textView.setLayoutParams(params);

        linearLayout.addView(textView);

        return linearLayout;*/


        /*  TextView textView = new TextView(getActivity());
        textView.setText("one");
        textView.setTextColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER);
        return textView;*/
        return binding.root
    }

    override fun onResume() {
        super.onResume()
        initView()
    }

    private fun initView() {
        binding.flGroup.post {
            var expandExClick = Rect()
            binding.tvClick.isEnabled = true
            binding.tvClick.setOnClickListener{
                Toast.makeText(activity,
                    "Touch occurred within ImageButton touch region.",
                    Toast.LENGTH_SHORT).show()

            }

            binding.tvClick.getHitRect(expandExClick)
            expandExClick.top += 100
            expandExClick.left += 100


            val touchDelegate = TouchDelegate(expandExClick, binding.tvClick)
//            binding.tvClick.parent.let {
//                (it as View).touchDelegate = touchDelegate
//            }
            if (View::class.java.isInstance(binding.tvClick.parent)) {
                (binding.tvClick.parent as View).touchDelegate = touchDelegate
            }
        }
    }
}